#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;


void classification(vector<string>& I, vector<int>& R) {
	map<int, map<int, string>> result;
	for (int i = 0; i < R.size(); i++) {
		int index = R[i];
		if (result.count(index))
			continue;
		string sub = to_string(index);
		for (int j = 0; j < I.size(); j++) {
			if (I[j].find(sub) != -1) {
				result[index][j] = I[j];
			}
		}
	}
	int printNum = 0;
	for (auto it : result) {
		int size = it.second.size();
		if (size != 0)
			printNum = printNum + 2 + size * 2;
	}
	cout << printNum << " ";
	for (auto element : result) {
		if (element.second.size() == 0)
			continue;
		else cout << element.first << " " << element.second.size() << " ";
		for (auto item : element.second) {
			cout << item.first << " " << item.second << " ";
		}
	}

}

int main() {
	int N;
	cin >> N;
	vector<string> I(N);
	for (int i = 0; i < N; i++)
		cin >> I[i];
	int M;
	cin >> M;
	vector<int> R(M);
	for (int i = 0; i < M; i++)
		cin >> R[i];
	classification(I, R);
}
